Getting Table Properties
Another common task is to get a row count of the tables in a particular database:
PS SQLSERVER:\SQL\<servername>\<instancename>\Databases\
AdventureWorks2008R2\Tables> Get-ChildItem .| Sort-Object -descending|Select-
Object -First 10| Format-Table -autosize Name,RowCountNote
Note
An easy-to-remember alias for Get-ChildItem is basically dir.
In the preceding example using the AdventureWorks2008R2 database, the top 10 tables with the highest row count value are returned.
Note
The preceding example shows
how many features are packaged within PowerShell, which applies not only
to SQL tables, but also to all .NET objects. Simply using Get-Item on a particular table returns only the default properties of Schema, Name, and Created. Piping something like Get-Item [table_name] to either Format-Table * or Get-Member exposes all the properties available for a particular object.
Cmdlet Example: Invoke-SqlCmd
The Invoke-SqlCmd
cmdlet was mentioned earlier. It will likely be the most commonly used
cmdlet currently provided. Here is a simple example using this cmdlet:
Invoke-sqlcmd -query "exec sp_help"
Using Invoke-SqlCmd, you
can simply pass any T-SQL–based query as a value to the cmdlet. The
preceding basic example is provided by running a basic built-in stored
procedure: sp_help.
This example demonstrates
several important issues, especially how powerful the provider can be.
Based on the location in the SQL provider, some of the values passed to
the cmdlet are automatically provided to the cmdlet by the provider
itself: the server and database to query aren’t required in the command
line.
Let’s consider this example a bit further and do a few extra things with it.
First, this cmdlet can accept input from the pipeline:
"exec sp_help"|ForEach-Object{Invoke-SqlCmd $_}
The preceding line demonstrates a few more issues that have already been discussed: the ForEach-Object cmdlet, the special variable $_, and also the way parameters can automatically match values to parameters even when the parameter name isn’t explicitly added to the command entered.
The sp_help stored procedure provides a lot of information. What if only the extended stored procedures were required?
When Get-Member is used, the members from this particular query are inspected, and it is determined that the Object_type property is the value that indicates what kind of stored procedure is being dealt with.
The query to get only extended stored procedures is now the following:
"exec sp_help"|ForEach-Object{Invoke-SqlCmd $_}| `
Where{$_.Object_type -eq "extended stored proc"}|Select Name
Finally, the output consists only of extended stored procedures.
Cmdlet Example: Invoke-PolicyEvaluation
Another one of the provided cmdlets is Invoke-PolicyEvaluation.
This cmdlet is used to specify a SQL Server Policy-Based Management
policy (or policies) that will be evaluated against the target server.
You can easily cycle through all the available policies and evaluate
each one, or simply provide a list of policies to evaluate separated by a
comma. Consider this example:
sl "C:\Program Files\Microsoft SQL Server\100\Tools\Policies\DatabaseEngine\1033"
Invoke-PolicyEvaluation -Policy "Database Auto Shrink.xml" -TargetServerName
"MSSQLSERVER"
The preceding command
returns the output to the console of the result of the policy
evaluation. By default, the particular policy passed to the cmdlet is
only checked. In other words, by default, properties are actually
changed so that they are now compliant with the policy.
Joining Columns
Quite often, databases provide a
table for users. Frequently, these users have their last name and first
name in separate columns. Because these are typically simple strings, a
feature, already discussed, allows two strings to be easily joined
together.
The following code snippet shows how two columns from the AdventureWorks2008R2 database are easily joined together from within the SQL minishell:
PS SQLSERVER:\SQL\D830\DEFAULT\databases\adventureworks2008r2> Invoke-SqlCmd
"Select * from Person.Person"| `
>> Select-Object -First 10|ForEach-Object{$_.LastName + ", " + $_.FirstName}
S´nchez, Ken
Duffy, Terri
Tamburello, Roberto
Walters, Rob
Erickson, Gail
Goldberg, Jossef
Miller, Dylan
Margheim, Diane
Matthew, Gigi
Raheem, Michael
Here, the first 10 records are selected, and then the LastName and FirstName values are combined together.
Retrieving an Entry
On occasion, you might need
to get a particular entry from a table. When the following code snippet
is run, an array is automatically returned:
PS SQLSERVER:\SQL\D830\DEFAULT\databases\adventureworks2008r2> Invoke-SqlCmd
"Select * from Person.Person"
PowerShell provides a simple
way to look at particular elements within an array. In the following
example, entry 100 is returned and then entries 95 to 105:
PS SQLSERVER:\SQL\D830\DEFAULT\databases\adventureworks2008r2> (Invoke-SqlCmd
"Select * from Person.Person")[100]
PS SQLSERVER:\SQL\D830\DEFAULT\databases\adventureworks2008r2> (Invoke-SqlCmd
"Select * from Person.Person")[95..105]
Note
The first element of an array is
the element zero; therefore, the first record in an array is retrieved
by referencing the element id [0].